package com.carleasoft.mps.adu.business.position.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.adu.business.position.bean.entity.AuthorityPositionEntity;
import com.carleasoft.mps.adu.business.position.bean.entity.AuthorityPositionGroupEntity;
import com.carleasoft.mps.adu.business.position.bean.po.AuthorityPositionPo;
import com.carleasoft.mps.adu.business.position.bean.vo.PositionVo;
import com.carleasoft.mps.adu.business.position.dao.AuthorityPositionDao;
import com.carleasoft.mps.adu.business.position.service.AuthorityPositionGroupService;
import com.carleasoft.mps.adu.business.position.service.AuthorityPositionService;
import com.carleasoft.mps.adu.common.constants.BusinessErrorEnum;
import com.carleasoft.mps.core.exceptions.BusinessException;
import com.carleasoft.mps.core.user.Constants;
import com.carleasoft.mps.core.user.ThreadUserContext;
import com.carleasoft.mps.core.user.UserCacheData;
import com.carleasoft.mps.security.permission.data.RequiresDataPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;


/**
 * <p>Description: position serviceImpl</p>
 *
 * @author ztm
 * @since 2020-05-12
 */
@Service
public class AuthorityPositionServiceImpl extends ServiceImpl<AuthorityPositionDao, AuthorityPositionEntity> implements AuthorityPositionService {
    @Autowired
    private AuthorityPositionGroupService positionGroupService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean addPosition(AuthorityPositionPo authorityPositionPo) {
        UserCacheData user = ThreadUserContext.getCurrentCacheUser();
        //查询重复
        if (StrUtil.isNotBlank(authorityPositionPo.getPositionGroupCode())){
            int count = this.count(Wrappers.<AuthorityPositionEntity>lambdaQuery()
                    .eq(AuthorityPositionEntity::getPositionGroupCode, authorityPositionPo.getPositionGroupCode())
                    .eq(AuthorityPositionEntity::getPositionName, authorityPositionPo.getPositionName()));
            if (count > 0) {
                throw new BusinessException(BusinessErrorEnum.DUPLICATE_POSITION_NUMBER);
            }
        }
        AuthorityPositionEntity positionEntity = BeanUtil.copyProperties(authorityPositionPo, AuthorityPositionEntity.class);
        positionEntity.setPositionCode(IdWorker.getIdStr());
        positionEntity.setPositionName(authorityPositionPo.getPositionName());
        positionEntity.setPositionGroupCode(authorityPositionPo.getPositionGroupCode());
        positionEntity.setCreatePersonCode(user.getUserCode());
        positionEntity.setCreatePersonName(user.getUserName());
        AuthorityPositionGroupEntity authorityPositionGroupEntity = this.positionGroupService.getOne(Wrappers.<AuthorityPositionGroupEntity>lambdaQuery().eq(AuthorityPositionGroupEntity::getPositionGroupCode, authorityPositionPo.getPositionGroupCode()));
        positionEntity.setDeptCode(authorityPositionGroupEntity.getDeptCode());
        boolean isOk = this.save(positionEntity);

        return isOk;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updatePosition(AuthorityPositionPo authorityPositionPo) {
        //查询重复
        int count = this.count(Wrappers.<AuthorityPositionEntity>lambdaQuery()
                .eq(AuthorityPositionEntity::getPositionGroupCode, authorityPositionPo.getPositionGroupCode())
                .eq(AuthorityPositionEntity::getPositionName, authorityPositionPo.getPositionName())
                .ne(AuthorityPositionEntity::getPositionCode, authorityPositionPo.getPositionCode()));
        if (count > 0) {
            throw new BusinessException(BusinessErrorEnum.DUPLICATE_POSITION_NUMBER);
        }
        AuthorityPositionEntity positionEntity = this.getById(authorityPositionPo.getId());
        if (positionEntity != null) {
            BeanUtil.copyProperties(authorityPositionPo, positionEntity);
        }
        positionEntity.setUpdateTime(LocalDateTime.now());
        boolean isOk = this.updateById(positionEntity);

        return isOk;
    }

    @Override
    @RequiresDataPermission(dataPerm = {@RequiresDataPermission.Scope(method = "pageList",scope = Constants.DATA_SCOPE_DEPT)})
    public Page selectList(Page page, AuthorityPositionPo authorityPositionPo) {
        return this.getBaseMapper().pageList(page, authorityPositionPo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(String positionCode) {
        return this.remove(Wrappers.<AuthorityPositionEntity>lambdaQuery()
                .eq(AuthorityPositionEntity::getPositionCode, positionCode));
    }

    @Override
    @RequiresDataPermission(dataPerm = {@RequiresDataPermission.Scope(method = "positionList",scope = Constants.DATA_SCOPE_DEPT)})
    public List<PositionVo> listPosition() {
        return this.getBaseMapper().positionList();
    }

    @Override
    public PositionVo findByCode(AuthorityPositionPo authorityPositionPo) {
        AuthorityPositionEntity positionEntity = this.getOne(Wrappers.<AuthorityPositionEntity>lambdaQuery()
                .eq(AuthorityPositionEntity::getPositionCode, authorityPositionPo.getPositionCode()));
        PositionVo positionVo = new PositionVo();
        if (positionEntity != null) {
            String positionGroupName = positionGroupService.getOne(Wrappers.<AuthorityPositionGroupEntity>lambdaQuery()
                    .eq(AuthorityPositionGroupEntity::getPositionGroupCode, positionEntity.getPositionGroupCode())).getPositionGroupName();
            BeanUtil.copyProperties(positionEntity, positionVo);
            positionVo.setPositionGroupName(positionGroupName);
        } else {
            positionVo = null;
        }
        return positionVo;
    }

    @Override
    public Boolean checkPosition(AuthorityPositionPo authorityPositionPo) {
        int count = this.count(Wrappers.<AuthorityPositionEntity>lambdaQuery().eq(AuthorityPositionEntity::getPositionName, authorityPositionPo.getPositionName()));
        if (count>0){
            return  false;
        }
        return true;
    }


}
